<template>
    <div>
        <el-dialog
        :title="isEdit?'修改':title"
        :visible.sync="dialogVisible"
        width="40%">
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="封面">
                <el-upload
                    class="upload-demo"
                    accept=".jpeg,.png,.jpg,.bmp,.gif,.webp"
                    :action="uploadUrl"
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :before-remove="beforeRemove"                    
                    :on-exceed="handleExceed"
                    :on-success="handleFileSuccess"
                    :headers="headers"
                    :file-list="form.fileList"
                    :limit="1"
                    list-type="picture-card"
                    >
                    <el-button size="small" type="primary">点击上传</el-button>
                    <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
                </el-upload>
            </el-form-item>
            <el-form-item label="演示视频">
                <el-upload
                    class="upload-demo"
                    :action="uploadUrl"
                    accept="video/*"
                    :on-preview="handleVideoPreview"
                    :on-remove="handleVideoRemove"
                    :before-remove="beforeRemove"                    
                    :on-exceed="handleExceed"
                    :headers="headers"
                    :file-list="form.videoList"
                    :on-success="handleVideoSuccess"
                    :before-upload="beforeVideoUpload"
                    :limit="1"
                    >
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传视频类型文件</div>
                </el-upload>
            </el-form-item>
            <el-form-item label="主题">
                <el-input v-model="form.theme" clearable></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
                <!-- <el-switch v-model="form.release"></el-switch> -->
                <el-switch v-model="form.isRelease" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item label="描述">
                <el-input type="textarea" v-model="form.remark" :show-word-limit="true" rows="4"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitHandle">确 定</el-button>
        </span>
        </el-dialog>

        <!-- 视频播放 -->
      <CoursePlayerDialog ref="coursePlayerDialog"></CoursePlayerDialog>
    </div>
</template>

<script>
    import product from '@/api/product/product'
    import { getToken } from '@/utils/auth'
    import CoursePlayerDialog from '@/views/course/coursePlayerDialog.vue'
    export default {
        name: 'CourseDialog',
        components: { CoursePlayerDialog },
        props: {
            title: {
                type: String,
                default:'新增'
            },
            
        },
        data () {
            return {
                dialogVisible:false,
                isEdit:false,//是否编辑
                form: {
                    id: '',
                    theme: '',
                    isRelease: '', // 发布标志，0-未发布，1-发布
                    remark: '',//描述
                    fileList:[],//封面
                    videoList:[],// 视频
                }
            }
        },
        computed: {
            uploadUrl(){
                return process.env.VUE_APP_BASE_API+'/public/file/upload'
            },
            headers() {
                return { Authorization: getToken() };
            },
        },
        mounted () {
            // alert("sfdsf");
            // for (let index = 0; index < this.fileList.length; index++) {
            //     const element = this.fileList[index];
            //     this.imageList.push(element.url);
            // }      
            // console.log(JSON.stringify(this.imageList));
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    if (element.id == file.id) {
                        fileList.splice(index, 1);
                        break;
                    }
                }
                this.form.fileList = fileList;
            },
            handleVideoRemove(file, fileList) {
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    if (element.id == file.id) {
                        fileList.splice(index, 1);
                        break;
                    }
                }
                this.form.videoList = fileList;
            },
            handlePreview(file) {
                console.log(file);
                // let urls = [];
                // for (let index = 0; index < this.fileList.length; index++) {
                //     const element = this.fileList[index];
                //     console.log(element.url);
                //     urls.push(element.url);
                // }
                this.$viewerApi({ images: [file.url] })

                // const viewer = this.$el.querySelector('.images').$viewer
                // viewer.show()
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
            //点击视频查看
            handleVideoPreview(file){
                this.$refs.coursePlayerDialog.videoUrl = file.url;
                this.$refs.coursePlayerDialog.dialogVisible = true;
                this.$refs.coursePlayerDialog.coursePlayerHandle();
            },
            beforeVideoUpload(file){
                const isLimit = file.size / 1024 / 1024 / 1024 < 1
                const isVideo = /video\/\w+/.test(file.type)
                if (!isLimit) {
                    this.$notify({
                    title: '上传视频大小不能超过 1G!',
                    type: 'warning'
                    })
                }
                if (!isVideo) {
                    this.$notify({
                        title: '只能上传视频文件!',
                        type: 'warning'
                    })
                }
                this.isValidate = isLimit && isVideo
                return isLimit && isVideo
            },
            handleFileSuccess(response, file, fileList) {//封面附件上传成功
                let fileList2 = [];
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    element.name = element.fileName;
                    element.response.data.name = element.response.data.fileName;
                    element.response.data.cover = '1';//是否封面
                    fileList2.push(element.response.data);
                }
                console.log(JSON.stringify(fileList2));
                this.form.fileList = fileList2;
            },
            handleVideoSuccess(response, file, fileList){//视频附件上传成功
                let fileList2 = [];
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    element.name = element.fileName;
                    element.response.data.name = element.response.data.fileName;
                    element.response.data.cover = '0';//是否封面
                    fileList2.push(element.response.data);
                }
                console.log(JSON.stringify(fileList2));
                this.form.videoList = fileList2;
            },
            submitHandle(){
                if (this.form.fileList.length == 0) {
                    this.$message.warning(`请上传封面图片`);
                    return;
                }
                if (this.form.videoList.length==0) {
                    this.$notify({ message: '请上传演示视频', type: 'warning' });
                    return;
                }
                let tbDemoMoldTakingFileList = [...this.form.fileList, ...this.form.videoList];

                var forms = {
                    theme: this.form.theme,
                    isRelease: this.form.isRelease, // 发布标志，0-未发布，1-发布
                    remark: this.form.remark,//描述
                    tbDemoMoldTakingFileList:tbDemoMoldTakingFileList,//附件
                    takingType:'1'
                };
                
                if (this.isEdit) {//修改
                    this.form.tbDemoMoldTakingFileList = tbDemoMoldTakingFileList;
                    product.tbDemoMoldTakingUpdate(this.form).then((res) => {
                        this.dialogVisible = false
                        this.$notify({ message: '操作成功', type: 'success'});
                        this.$emit("submitSuccess")
                    }).catch(() => {})
                }else{
                    product.tbDemoMoldTakingCreate(forms).then((res) => {
                        this.dialogVisible = false
                        this.$notify({ message: '操作成功', type: 'success'});
                        this.$emit("submitSuccess")
                    }).catch(() => {})
                }
            }
        }
    }
</script>

<style lang="scss">

</style>